$offlisting

$hidden  gams2txt.gms    Data export routine.
$hidden
$hidden  $...include gams2txt id
$hidden
$hidden  works for parameters and variables
$hidden
$hidden  First invocation must be outside of a loop or if block.
$hidden
$hidden  Use a blank invocation (without an id) to initialize.
$hidden

*       Skip compilation is there is a pre-existing program error:

$if not errorfree $exit

*       If we are already initialized, skip declarations:

$if setglobal gams2txt $goto start

*       Install an environment variable indicating that the
*       gams2txt subsystem is in operation:

$setglobal gams2txt='yes'

*       Declare temporary variables used for data transfer.

alias(u__1,u__2,u__3,u__4, u__5,u__6,u__7,u__8,u__9,u__10,*);
set gms2txt1
    gms2txt2
    gms2txt3
    gms2txt4
    gms2txt5
    gms2txt6
    gms2txt7
    gms2txt8
    gms2txt9
    gms2txt10;

$if errorfree $goto start
$onlisting

*       An error has occured during the initial invocation of
*       gams2txt. There are two reasons than an error may have
*       been generated here:

*       (i) The first invocation of gams2txt is inside an if or loop.
*       The simple solution for this problem is to use initialize with
*       "$libinclude gams2txt" before entering the loop or if.

*       (ii) User program has used one of the gams2txt reserved names,
*       u__n and gams2txtn for n = 0,1,2,...,10.

$abort 'Error on initial $libinclude gams2txt.'

$label start

*       Blank invocation -- nothing to do:

$if %1a == a $exit

*       See that the item is declared and defined:

$if declared %1    $goto declared
$error Error in gams2txt: identfier %1 is undeclared.
$exit

$label declared
$if defined %1     $goto defined
$error Error in gams2txt: identfier %1 is undefined.
$exit

$label defined

*       Set up temporary sets for processing:

$if dimension 0 %1 $goto scalar
$if dimension 1  %1 $set r 'u__1' set s gms2txt1
$if dimension 2  %1 $set r 'u__1,u__2' set s gms2txt2
$if dimension 3  %1 $set r 'u__1,u__2,u__3' set s gms2txt3
$if dimension 4  %1 $set r 'u__1,u__2,u__3,u__4' set s gms2txt4
$if dimension 5  %1 $set r 'u__1,u__2,u__3,u__4,u__5' set s gms2txt5
$if dimension 6  %1 $set r 'u__1,u__2,u__3,u__4,u__5,u__6' set s gms2txt6
$if dimension 7  %1 $set r 'u__1,u__2,u__3,u__4,u__5,u__6,u__7' set s gms2txt7
$if dimension 8  %1 $set r 'u__1,u__2,u__3,u__4,u__5,u__6,u__7,u__8' set s gms2txt8
$if dimension 9  %1 $set r 'u__1,u__2,u__3,u__4,u__5,u__6,u__7,u__8,u__9' set s gms2txt9
$if dimension 10 %1 $set r 'u__1,u__2,u__3,u__4,u__5,u__6,u__7,u__8,u__9,u__10' set s gms2txt10

$onuni
file.lw =  0;
file.nr =  2;
file.nw = 22;
file.nd = 13;
file.tf =  3;

%s%(%r%)$%1(%r%) = yes;
$if not setglobal prefix $goto default
loop(%s%, put %prefix%,%s%.te(%s%),%1(%s%)/;);
$goto cleanup

$label default
loop(%s%, put %s%.te(%s%),%1(%s%)/;);

$label cleanup
$offuni
option clear=%s%;
$exit

$label scalar
file.nr =  2;
file.nw = 22;
file.nd = 13;
file.tf =  3;
$if not setglobal prefix $goto single
put %prefix%,%1/;
$exit

$label single
put %1/;
